package com.bw.my.mvvm.user.viewmodel;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;

import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.fleld.UIField;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;
import com.bw.my.mvvm.model.entity.UserEntity;
import com.bw.my.mvvm.model.entity.usergreendao.UserDaoEntity;
import com.bw.my.mvvm.user.LoginActivity;
import com.bw.my.mvvm.user.model.RegModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class RegViewModel extends BaseViewModel<RegModel> {

    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> note = new ObservableField<>();
    public ObservableField<String> lock = new ObservableField<>();
    public ObservableField<String> lock1 = new ObservableField<>();
    @Override
    protected void result(BaseEntity entity) {

            UserEntity userEntity = (UserEntity) entity;

            UserDaoEntity userDao = new UserDaoEntity();
            userDao.insert(userEntity);
            if (entity.getMessage().equals("注册成功")){
                EventBus.getDefault().post("注册成功");

                finishPage();
            }else {
                LogUtils.e(entity.getMessage());
            }
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void back(){
        finishPage();
    }

    public void regClick(){
        String s = lock.get();
        String s1 = lock1.get();

        if (s.equals(s1)){

            HashMap<String,Object> map = new HashMap<>();
            map.put("mobile",phone.get());
            map.put("pwd",s);
            map.put("verifyCode",note.get());
            RequestBody body = m.createBody(map);
            m.requestReg(body).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this);
        }
    }


}
